package com.xbongbong.sso.enums;

import com.xbongbong.sso.pojo.OauthFieldPojo;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 授权类型枚举
 */
@Getter
public enum OauthFieldEnum {
    /**
     *
     */
    AUTH_CODE("系统参数", "auth_code", "Auth Code", "IDP生成返回的授权码"),
    ACCESS_TOKEN("系统参数","access_token", "Access Token", "IDP生成返回的令牌"),
    IDENTITY_AUTHENTICATION("系统参数","identity_authentication", "Identity Authentication", "IDP返回的用户唯一码，用于此字段对应销帮帮用户。"),
//    return_uri("系统参数","return_uri", ""),
    CLIENT_ID("IDP参数","client_id", "Client Id", "IDP分配的客户端id"),
    CLIENT_SECRET("IDP参数","client_secret", "Client Secret", "IDP分配的客户端密钥"),
    REDIRECT_URL("SP参数","redirect_url", "Redirect Url", "接收IDP生成的授权码地址"),
//    logout_url("SP参数","logout_url", ""),
    UNKNOWN("未知","未知", "", ""),
    ;

    private String type;

    private String attr;

    private String name;

    private String description;

    OauthFieldEnum(String type, String attr, String name, String description) {
        this.type = type;
        this.attr = attr;
        this.name = name;
        this.description = description;
    }

    /**
     * 根据attr获取枚举
     * @return
     */
    public static OauthFieldEnum getByAttr(String attr) {
        if (attr == null) {
            return UNKNOWN;
        }
        for (OauthFieldEnum cache : values()) {
            if (Objects.equals(cache.getAttr(), attr)) {
                return cache;
            }
        }
        return UNKNOWN;
    }
    public static List<OauthFieldPojo> getSystemList() {
        List<OauthFieldEnum> oauthFieldEnums = Arrays.asList(AUTH_CODE, ACCESS_TOKEN, IDENTITY_AUTHENTICATION,
                CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
        List<OauthFieldPojo> systemList = new ArrayList<>();
        for (OauthFieldEnum oauthField : oauthFieldEnums) {
            systemList.add(new OauthFieldPojo(oauthField.getAttr(), oauthField.getName(), oauthField.getDescription()));
        }
        return systemList;
    }
}
